<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: DataSchema.JSON</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: DataSchema.JSON</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>DataSchema.JSON normalizes arbitrary JSON data against a given schema into an object with known properties.</p>
</div>

<div class="example yui3-skin-sam">
    <style scoped>
/* custom styles for this example */
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
</style>

<form id="demo">
    <h4>Basic example</h4>
    <h6>Data</h6>
    <pre>
{
    "total":10,
    "results":[
        {"n":1, "fname":"George", "lname":"Washington"},
        {"n":2, "fname":"John", "lname":"Adams"},
        {"n":3, "fname":"Thomas", "lname":"Jefferson"},
        {"n":4, "fname":"James", "lname":"Madison"},
        {"n":5, "fname":"James", "lname":"Monroe"},
        {"n":6, "fname":"John", "mname":"Quincy", "lname":"Adams"},
        {"n":7, "fname":"Andrew", "lname":"Jackson"},
        {"n":8, "fname":"Martin", "lname":"Van Buren"},
        {"n":9, "fname":"William", "mname":"Henry", "lname":"Harrison"},
        {"n":10, "fname":"John", "lname":"Tyler"}
    ]
}
    </pre>
    
    <h6>Schema</h6>
    <pre>
{
    metaFields: {total:"total"},
    resultListLocator: "results",
    resultFields: [{key:"n"}, {key:"fname"}, {key:"lname"}]
}
    </pre>

    <h6>Normalized data</h6>
    <input type="button" id="demo_apply_basic" value="Apply schema =>">
    <div id="demo_output_basic" class="output"></div>

    <h4>Complex example</h4>
    <h6>Data</h6>
    <pre>
{
    "profile":{
        "current":160,
        "target":150
    },
    "reference": [
        {
            "category":"exercise",
            "type":"expenditure",
            "activities":[
                {"name":"biking", "calories":550},
                {"name":"golf", "calories":1000},
                {"name":"running", "calories":650},
                {"name":"swimming", "calories":650},
                {"name":"walking", "calories":225}
            ]
        },
        {
            "category":"nutrition",
            "type":"intake",
            "fruit":[
                {"name":"apple", "calories":70},
                {"name":"banana", "calories":70},
                {"name":"orange", "calories":90},
            ],
            "vegetables":[
                {"name":"baked potato", "calories":150},
                {"name":"broccoli", "calories":50},
                {"name":"green beans", "calories":30}
            ]
        }
    ],
    "program": [
        {
            "category":"exercise",
            "schedule":[
                {"day":"sunday", "activity":"swimming"},
                {"day":"monday", "activity":"running"},
                {"day":"tuesday", "activity":"biking"},
                {"day":"wednesday", "activity":"running"},
                {"day":"thursday", "activity":"swimming"},
                {"day":"friday", "activity":"running"},
                {"day":"saturday", "activity":"golf"}
            ]
        },
        {
            "category":"diet",
            "schedule":[
            ]
        }
    ]
}
    </pre>

    <h6>Schema</h6>
    <pre>
{
    metaFields: {current:"profile.current", target:"profile.target",
        reference:"reference[0].activities"},
    resultListLocator: "program[0].schedule",
    resultFields: [{key:"day"}, {key:"activity"}]
}
    </pre>

    <h6>Normalized data</h6>
    <input type="button" id="demo_apply_complex" value="Apply schema =>">
    <div id="demo_output_complex" class="output"></div>
</form>


<script>
YUI().use("dump", "node", "dataschema-json", function (Y) {
    Y.on("click", function(e){
        var data_in = {
                "total":10,
                "results":[
                    {"n":1, "fname":"George", "lname":"Washington"},
                    {"n":2, "fname":"John", "lname":"Adams"},
                    {"n":3, "fname":"Thomas", "lname":"Jefferson"},
                    {"n":4, "fname":"James", "lname":"Madison"},
                    {"n":5, "fname":"James", "lname":"Monroe"},
                    {"n":6, "fname":"John", "mname":"Quincy", "lname":"Adams"},
                    {"n":7, "fname":"Andrew", "lname":"Jackson"},
                    {"n":8, "fname":"Martin", "lname":"Van Buren"},
                    {"n":9, "fname":"William", "mname":"Henry", "lname":"Harrison"},
                    {"n":10, "fname":"John", "lname":"Tyler"}
                ]
            },
            schema = {
                metaFields: {total:"total"},
                resultListLocator: "results",
                resultFields: [{key:"n"}, {key:"fname"}, {key:"lname"}] // Or simply: ["n", "fname", "lname"]
            };
        Y.one("#demo_output_basic").setContent(Y.dump(Y.DataSchema.JSON.apply(schema, data_in)));
    }, "#demo_apply_basic");

    Y.on("click", function(e){
        var data_in = {
                "profile":{
                    "current":160,
                    "target":150
                },
                "reference": [
                    {
                        "category":"exercise",
                        "type":"expenditure",
                        "activities":[
                            {"name":"biking", "calories":550},
                            {"name":"golf", "calories":1000},
                            {"name":"running", "calories":650},
                            {"name":"swimming", "calories":650},
                            {"name":"walking", "calories":225}
                        ]
                    },
                    {
                        "category":"nutrition",
                        "type":"intake",
                        "fruit":[
                            {"name":"apple", "calories":70},
                            {"name":"banana", "calories":70},
                            {"name":"orange", "calories":90},
                        ],
                        "vegetables":[
                            {"name":"baked potato", "calories":150},
                            {"name":"broccoli", "calories":50},
                            {"name":"green beans", "calories":30}
                        ]
                    }
                ],
                "program": [
                    {
                        "category":"exercise",
                        "weekly schedule":[
                            {"day":"sunday", "activity":"swimming"},
                            {"day":"monday", "activity":"running"},
                            {"day":"tuesday", "activity":"biking"},
                            {"day":"wednesday", "activity":"running"},
                            {"day":"thursday", "activity":"swimming"},
                            {"day":"friday", "activity":"running"},
                            {"day":"saturday", "activity":"golf"}
                        ]
                    },
                    {
                        "category":"diet",
                        "schedule":[
                        ]
                    }
                ]
            },
            schema = {
                metaFields: {current:"profile.current", target:"profile.target", reference:"reference[0].activities"},
                resultListLocator: "program[0]['weekly schedule']",
                resultFields: [{key:"day"}, {key:"activity"}] // Or simply: ["day", "activity"]
            };
        Y.one("#demo_output_complex").setContent(Y.dump(Y.DataSchema.JSON.apply(schema, data_in)));
    }, "#demo_apply_complex");
});
</script>

</div>

<p>In order to use DataSchema.JSON, input data must be a JavaScript object.</p>

<pre class="code prettyprint">YUI().use(&quot;dataschema-json&quot;, function(Y) {
    var data_in = {
            total:10,
            results:[
                {n:1, fname:&quot;George&quot;, lname:&quot;Washington&quot;},
                {n:2, fname:&quot;John&quot;, lname:&quot;Adams&quot;},
                {n:3, fname:&quot;Thomas&quot;, lname:&quot;Jefferson&quot;},
                {n:4, fname:&quot;James&quot;, lname:&quot;Madison&quot;},
                {n:5, fname:&quot;James&quot;, lname:&quot;Monroe&quot;},
                {n:6, fname:&quot;John&quot;, mname:&quot;Quincy&quot;, lname:&quot;Adams&quot;},
                {n:7, fname:&quot;Andrew&quot;, lname:&quot;Jackson&quot;},
                {n:8, fname:&quot;Martin&quot;, lname:&quot;Van Buren&quot;},
                {n:9, fname:&quot;William&quot;, mName:&quot;Henry&quot;, lname:&quot;Harrison&quot;},
                {n:10, fname:&quot;John&quot;, lname:&quot;Tyler&quot;}
            ]
        },
        schema = {
            metaFields: {total:&quot;total&quot;},
            resultListLocator: &quot;results&quot;,
            &#x2F;&#x2F; Or simply: [&quot;n&quot;, &quot;fname&quot;, &quot;lname&quot;]
            resultFields: [{key:&quot;n&quot;}, {key:&quot;fname&quot;}, {key:&quot;lname&quot;}]
        },
        data_out = Y.DataSchema.JSON.apply(schema, data_in));

    alert(data_out);
});</pre>


<p>The data itself can get fairly complex, with deeply nested arrays and objects. In your schema, you can use dot notation and the array-index syntax to define these locations. When necessary, you can also use object-bracket notation to define locations that might otherwise be invalid with dot notation.</p>

<pre class="code prettyprint">YUI().use(&quot;dataschema-json&quot;, function(Y) {
        var data_in = {
            &quot;profile&quot;:{
                &quot;current&quot;:160,
                &quot;target&quot;:150
            },
            &quot;reference&quot;: [
                {
                    &quot;category&quot;:&quot;exercise&quot;,
                    &quot;type&quot;:&quot;expenditure&quot;,
                    &quot;activities&quot;:[
                        {&quot;name&quot;:&quot;biking&quot;, &quot;calories&quot;:550},
                        {&quot;name&quot;:&quot;golf&quot;, &quot;calories&quot;:1000},
                        {&quot;name&quot;:&quot;running&quot;, &quot;calories&quot;:650},
                        {&quot;name&quot;:&quot;swimming&quot;, &quot;calories&quot;:650},
                        {&quot;name&quot;:&quot;walking&quot;, &quot;calories&quot;:225}
                    ]
                },
                {
                    &quot;category&quot;:&quot;nutrition&quot;,
                    &quot;type&quot;:&quot;intake&quot;,
                    &quot;fruit&quot;:[
                        {&quot;name&quot;:&quot;apple&quot;, &quot;calories&quot;:70},
                        {&quot;name&quot;:&quot;banana&quot;, &quot;calories&quot;:70},
                        {&quot;name&quot;:&quot;orange&quot;, &quot;calories&quot;:90},
                    ],
                    &quot;vegetables&quot;:[
                        {&quot;name&quot;:&quot;baked potato&quot;, &quot;calories&quot;:150},
                        {&quot;name&quot;:&quot;broccoli&quot;, &quot;calories&quot;:50},
                        {&quot;name&quot;:&quot;green beans&quot;, &quot;calories&quot;:30}
                    ]
                }
            ],
            &quot;program&quot;: [
                {
                    &quot;category&quot;:&quot;exercise&quot;,
                    &quot;weekly schedule&quot;:[
                        {&quot;day&quot;:&quot;sunday&quot;, &quot;activity&quot;:&quot;swimming&quot;},
                        {&quot;day&quot;:&quot;monday&quot;, &quot;activity&quot;:&quot;running&quot;},
                        {&quot;day&quot;:&quot;tuesday&quot;, &quot;activity&quot;:&quot;biking&quot;},
                        {&quot;day&quot;:&quot;wednesday&quot;, &quot;activity&quot;:&quot;running&quot;},
                        {&quot;day&quot;:&quot;thursday&quot;, &quot;activity&quot;:&quot;swimming&quot;},
                        {&quot;day&quot;:&quot;friday&quot;, &quot;activity&quot;:&quot;running&quot;},
                        {&quot;day&quot;:&quot;saturday&quot;, &quot;activity&quot;:&quot;golf&quot;}
                    ]
                },
                {
                    &quot;category&quot;:&quot;diet&quot;,
                    &quot;schedule&quot;:[
                    ]
                }
            ]
        },
        schema = {
            metaFields: {current:&quot;profile.current&quot;, target:&quot;profile.target&quot;,
                reference:&quot;reference[0].activities&quot;},
            resultListLocator: &quot;program[0][&#x27;weekly schedule&#x27;]&quot;,
            &#x2F;&#x2F; Or simply: [&quot;day&quot;, &quot;activity&quot;]
            resultFields: [{key:&quot;day&quot;}, {key:&quot;activity&quot;}]
        },
        data_out = Y.DataSchema.Array.apply(schema, data_in));

    alert(data_out);
});</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Schema parsing a JavaScript array.">
                                        <a href="dataschema-array.html">DataSchema.Array</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Schema parsing JSON data.">
                                        <a href="dataschema-json.html">DataSchema.JSON</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Schema parsing XML data.">
                                        <a href="dataschema-xml.html">DataSchema.XML for XML Data</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Schema parsing data held in TABLE elements.">
                                        <a href="dataschema-table.html">DataSchema.XML for HTML Tables</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Schema parsing delimited plain-text data.">
                                        <a href="dataschema-text.html">DataSchema.Text</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing data into specified types as the schema is being applied.">
                                        <a href="dataschema-parsing.html">Enforcing DataTypes</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements.">
                                        <a href="../datasource/datasource-local.html">DataSource.Local</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Get DataSource, which manages retrieval of data from remote sources via the Get Utility, can be useful for accessing data from cross-domain servers without the need for a proxy.">
                                        <a href="../datasource/datasource-get.html">DataSource.Get</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility.">
                                        <a href="../datasource/datasource-io.html">DataSource.IO</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Function DataSource, which manages retrieval of data from a JavaScript function, provides a highly customizeable mechanism for implementer-defined data retrieval algorithms">
                                        <a href="../datasource/datasource-function.html">DataSource.Function</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
                                        <a href="../datasource/datasource-caching.html">DataSource with Caching</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
                                        <a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
